//
//  insertDictionaryViewController.swift
//  EnglishDictionary
//
//  Created by QFX on 2024/2/26.
//

import UIKit
import MBProgressHUD

class insertDictionaryViewController: UIViewController {
    var word:Word?
    var sentence:Sentence?
    @IBOutlet weak var textFieldView: UIView!
    @IBOutlet weak var explainTextView: UITextView!
    @IBOutlet weak var addWordTextField: UITextField!
    @IBOutlet weak var finishButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = (self.word == nil && self.sentence == nil) ? "添加" : "编辑"
        
        if let word = self.word {
            self.addWordTextField.text = word.word
            self.explainTextView.text = word.st
        }else if let sentence = self.sentence {
            self.addWordTextField.text = sentence.sentence
            self.explainTextView.text = sentence.st
        }
        self.textFieldView.layer.cornerRadius = 10
        self.textFieldView.clipsToBounds = true
        self.textFieldView.layer.borderWidth = 1
        self .textFieldView.layer.borderColor = UIColor.black.cgColor
        
        self.explainTextView.layer.cornerRadius = 10
        self.explainTextView.clipsToBounds = true
        self.explainTextView.layer.borderWidth = 1
        self .explainTextView.layer.borderColor = UIColor.black.cgColor

        self.finishButton.layer.cornerRadius = 10
        self.finishButton.clipsToBounds = true
    }
    

    @IBAction func finish(_ sender: Any) {
        if self.addWordTextField.text?.count == 0 {
            TRHUD.showError("输入添加单词、句子语法等")
             return
        }
        if self.explainTextView.text?.count == 0 {
            TRHUD.showError("输入解义")
            return
        }
        
        if (self.word == nil && self.sentence == nil) {
            let actionSheet = VHLActionSheetView(title: "选择你要添加的类别", cancelButtonTitle: "取消", otherButtonTitles: ["Words","Sentences"]) { av, buttonType in
                if case let .other(index) = buttonType {
                     if index == 0 {
                         self.selectAddFold(cate: "Words")
                     } else if index == 1 {
                         self.selectAddFold(cate: "Sentences")
                     }
                }
            }
            actionSheet.show()
        }else{
            if self.word != nil{
                self.word?.word = self.addWordTextField.text
                self.word?.st = self.explainTextView.text
                try?self.word? .saveOrUpdate()
                TRHUD.showSuccess("编辑成功")
                kNOTIFY_POST(name: "cpSuccess", object: nil)
                self.navigationController?.popViewController(animated: true)
            } else if self.sentence != nil{
                self.sentence?.sentence = self.addWordTextField.text
                self.sentence?.st = self.explainTextView.text
                try?self.sentence? .saveOrUpdate()
                TRHUD.showSuccess("编辑成功")
                kNOTIFY_POST(name: "cpSuccess", object: nil)
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    func addSubFold(fold:String,cate:String){
        if cate == "Words"{
            let word = Word()
            word.word = self.addWordTextField.text
            word.st = self.explainTextView.text
            word.fold = fold
            try?word .saveOrUpdate()
        }else if cate == "Sentences"{
            let sentence = Sentence()
            sentence.sentence = self.addWordTextField.text
            sentence.st = self.explainTextView.text
            sentence.fold = fold
            try?sentence .saveOrUpdate()
        }
        TRHUD.showSuccess("添加成功")
        self.navigationController?.popViewController(animated: true)
    }
    

    func selectAddFold(cate:String){
        var foldx:[String] = []
        if  let folds = Fold.getFolds(parentFold: cate),folds.count > 0{
            for fold in folds {
                let foldName = fold.fold
                foldx.append(foldName!)
            }
        }

        let actionSheet = VHLActionSheetView(title: "选择你要添加的目录", cancelButtonTitle: "取消", otherButtonTitles: foldx) { av, buttonType in
            if case let .other(index) = buttonType {
                let foldName = foldx[index]
                var foldx1:[String] = []
                if let folds = Fold.getFolds(parentFold: foldName),folds.count > 0{
                    for fold in folds {
                        let foldName = fold.fold
                        foldx1.append(foldName!)
                    }
                    let actionSheet1 = VHLActionSheetView(title: "选择你要添加的目录", cancelButtonTitle: "取消", otherButtonTitles: foldx1) { av, buttonType in
                        if case let .other(index) = buttonType {
                            let foldName = foldx1[index]
                            self.addSubFold(fold: foldName, cate: cate)
                        }
                    }
                    actionSheet1.show()
                }else{
                    self.addSubFold(fold: foldName, cate: cate)
                }
            }
        }
        actionSheet.show()
    }
}
